<?php
include 'load.php';


$pubcol = $_POST['pub_col'];
$pubsite = $_POST['pub_site'];
$pub_filter = $_POST['pub_filter'];
$filter_type = $_POST['filter_type'];
$pub_reg = $_POST['pub_reg'];
$task_start = $_POST['task_start'];
$task_name = $_POST['task_name'];
$rss_site = $_POST['rsssite'];
$user_id = $_POST['userid'];
$task_id = $_POST['task_id'];


$rss_task = array('title'=> $task_name, 'rss_url'=>$rss_site, 'forward_content'=>$pubcol,
					'forward_site'=>$pubsite, 'forward_regularly'=>$pub_reg, 'filter_type'=> $pub_reg=='pub'?1:0,
					'create_time'=>gmdate("Y-m-d\TH:i:s\Z"), 'status'=>$task_start, 'uid'=>$user_id);

$result = array();

if($task_id == ''){
	$count = $snsdb->get_var('select count(*) from sns_rss_task where rss_url="'.$rss_site.'" and uid='.$user_id);
	if($count != '0'){
		$result['code'] = 'failure';
		$result['msg'] = '您已经存在一个该地址的任务！';
	}else{
		$id = uniqid();
		$rss_task['id'] = $id;
		$snsdb->insert('sns_rss_task', $rss_task);
		if (is_array($pub_filter)){
			foreach ($pub_filter as $filter){
				if(trim($filter['value']) != ''){
					$filter['id'] = uniqid();
					$filter['task_id'] = $id;
					$snsdb->insert('sns_task_filter', $filter);
				}
			}
		}
		
		$result['code'] = 'success';
	}
}else{
	$rss_task['status']  = 3;
	$snsdb->update("sns_rss_task", $rss_task, array("id"=>$task_id));
	if (is_array($pub_filter)){
			foreach ($pub_filter as $filter){
				if(trim($filter['value']) != ''){
					$filter['id'] = uniqid();
					$filter['task_id'] = $task_id;
					$snsdb->insert('sns_task_filter', $filter);
				}
			}
		}
	$result['code'] = 'success';
}

echo json_encode($result);
?>